package com.kgc.config;

import com.kgc.interceptor.HelloInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: luo
 * @date: 2024/4/6 21:08
 * @Description:
 * @Version: 1.0
 */
@Configuration
public class SpringConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/upload.html").setViewName("upload");
    }

    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HelloInterceptor())
                .addPathPatterns("/**")//要拦截的路径
                .excludePathPatterns(//排除了不要拦的路径
                        "/upload.html",
                        "/emp/**",
                        "/emp/upload",
                        "/user/login",
                        "/css/**",
                        "/images/**",
                        "/js/**");//不要拦截的路径
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //允许跨域访问的路径
                .allowedOriginPatterns("*") //允许跨域访问的源
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS") //允许请求的方式
                .allowedHeaders("*") //允许头部设置
                .allowCredentials(true) //是否发送cookie
                .maxAge(10000); //最大检测时间
    }
}
